package com.wl.myscalajob

class User(){
  var name:String = _
  var score = Array(0,0,0)

  def showFist(userBox:Int): Int ={
    var box = 0
    userBox match {
      case 1 => {box = 1 ; println("你出拳:剪刀")}
      case 2 => {box = 2 ; println("你出拳:石头")}
      case 3 => {box = 3 ; println("你出拳:布")}
      case _ => {box = 3;println("输入不符合规范,默认出布!")}
    }

    box
  }

}

class Computer(){
  var name:String = _
  var score = Array(0,0,0)


  def showFist(): Int ={
    println(s"${name}出拳!")
    val randomBox = scala.util.Random.nextInt(3) + 1

    randomBox match {
      case 1 => println(s"${name}出拳:剪刀")
      case 2 => println(s"${name}出拳:石头")
      case 3 => println(s"${name}出拳:布")
    }
    randomBox
  }

}

class Game(user:User,computer:Computer){
  var num =0

  def startGame(): Unit ={
    println("请出拳! 1.剪刀 2.石头 3.布")
    val userBoxNum = scala.io.StdIn.readInt()

    //出拳
    val userBox = user.showFist(userBoxNum)
    val computerBox = computer.showFist()

    //平局
    if(userBox == computerBox){
      user.score(1) += 1
      computer.score(1) += 1
      println("结果：和局！下次继续努力！")
    }else if((userBox - computerBox) == -1 || (userBox - computerBox) == 2){
      user.score(2) += 1
      computer.score(0) += 1
      println("结果：很遗憾你输了！下次继续努力！")
    }else{
      user.score(0) += 1
      computer.score(2) += 1
      println("结果：恭喜，你赢啦！")
    }

    num += 1
    println("是否开始下一轮(y/n)")
    val startOrEnd = scala.io.StdIn.readLine()
    if(startOrEnd.equals("y")){
      startGame()
    }else{


      println("退出游戏！")
      println("---------------------------------------")
      println(s"${computer.name} VS ${user.name}")
      println(s"对战次数$num")
      println("\n \n")
      println("姓名\t分数\t胜局\t和局\t负局")
      println(s"${user.name}  \t${user.score(0)*2 + user.score(1)*1}  \t\t${user.score(0)}  \t\t${user.score(1)}  \t\t${user.score(2)} ")
      println(s"${computer.name}\t${computer.score(0)*2 + computer.score(1)*1}  \t\t${computer.score(0)}  \t\t${computer.score(1)}  \t\t${computer.score(2)}")
    }

  }


}

object Job2 {
  def main(args: Array[String]): Unit = {
    println("----------欢迎进入游戏世界----------")
    println("**********************************")
    println("***********猜拳开始****************")
    println("**********************************")

    println("请选择你对战的角色:(1.刘备 2.关羽 3.张飞)")
    var comRoleNum = scala.io.StdIn.readInt()
    var flag1 = true
    var comRoleName = ""
    while(flag1) {
      if (comRoleNum != 1 && comRoleNum != 2 && comRoleNum != 3) {
        println("输入有误请重新输入！！！")
        comRoleNum = scala.io.StdIn.readInt()
        flag1 = true
      }else{
        flag1 = false
      }
    }

    comRoleName = comRoleNum match {
      case 1 => "刘备"
      case 2 => "关羽"
      case _ => "张飞"
    }
    println(s"你选择了与${comRoleName}对战\n要开始了吗？y/n")

    val startOrEnd = scala.io.StdIn.readLine()
    if(startOrEnd.equals("y")){

      val user = new User()
      user.name = "wl"

      val computer = new Computer()
      computer.name = comRoleName

      val game = new Game(user, computer)
      game.startGame()



    }else{
      return
    }




  }
}
